package com.xpgk.mvc.ddc.zydj.ywxg;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.ddc.ddc_commomUtil.DDC_CommonUtilService;
import com.xpgk.mvc.ddc.ddc_commomUtil.PictureService;
import com.xpgk.mvc.ddc.entity.Business;
import com.xpgk.mvc.ddc.entity.Credentials;
import com.xpgk.mvc.ddc.entity.Owner;
import com.xpgk.mvc.ddc.entity.Plate;
import com.xpgk.mvc.ddc.entity.Vehicle;
import com.xpgk.mvc.ddc.entity.VehicleParemeters;
import com.xpgk.mvc.ddc.statistic.DailyStasticService;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.user.User;

public class ZydjYwxgController  extends  BaseController{
	private DailyStasticService dailyStasticService;
	private PictureService pictureService = new PictureService();
	private DDC_CommonUtilService commonUtilService = new DDC_CommonUtilService();
	
	public void index() {
		if(!getCUserOrganIds().equals("f73bfba518ff4048afbdc006e3eda906")){
			setAttr("orgid", getCUserOrganIds());
		}
		render("ywxglist.html");
	}
	
	public  void  getlist(){
		String json = paging(splitPage, Business.sqlId_splitPageSelect,Business.sqlId_splitPageFrom);
		renderJson(json);
	}
	
	public  void  details(){
		ids=getPara("ids");
		Business business = Business.dao.findById(ids);
		setAttr("business", business);
		setAttr("owner2", Owner.dao.findById(business.get("OLDOWNERIDS")));
		setAttr("owner", Owner.dao.findById(business.get("NEWOWNERIDS")));
		setAttr("vehicleparemeters", VehicleParemeters.dao.findById(business.get("NEWPAREMETERSIDS")));
		setAttr("plate", Plate.dao.findById(business.get("NEWPLATEIDS")));
		setAttr("vehicle", Vehicle.dao.findById(business.get("VEHICLEIDS")));
		setAttr("credentials", Credentials.dao.findById(business.get("NEWCREDENTIALSIDS")));
		render("details.html");
	}

	@Before(Tx.class)
	public  void  nextprocess(){
		String ids = getPara("ids");
		Business business = Business.dao.findById(ids);
		User user = getCUser();
		String jbrids = user.getStr(User.column_ids);
		String jbr = user.getStr(User.column_name);
		String organids = user.getStr(User.column_organizationids);
		Organization organization = Organization.dao.findById(organids);
		String organ = organization.getStr(Organization.column_name);
		SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");
		business.set(Business.column_YWXGDATE, sdf.format(new Date()));
		business.set(Business.column_YWXGJBRIDS, jbrids);
		business.set(Business.column_YWXGJBR, jbr);
		business.set(Business.column_STATUS,"2");
		boolean result = business.update();
		result = dailyStasticService.saveDailyStastic(organids,organ,sdf.format(new Date()),"2","2");
		renderJson(result);
	}
	
	@Before(Tx.class)
	@SuppressWarnings("static-access")
	public  void  update(){	
		RenderBean result = new RenderBean();
		
		try {
			User user = getCUser();
			String jbrids = user.getStr(User.column_ids);
			String jbr = user.getStr(User.column_name);
			
			SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");
			
//			Owner  owner = getModel(Owner.class,"owner");
//			owner.update();
			
			Vehicle vehicle = getModel(Vehicle.class,"vehicle");
			Plate  plate = getModel(Plate.class,"plate");
			VehicleParemeters  vehicleparemeters = getModel(VehicleParemeters.class,"vehicleparemeters");
			
			Vehicle temp = vehicle.findById(vehicle.get("IDS"));
			List<Plate> lp = commonUtilService.getPlateByHPHM(plate.get("HPHM").toString());
			if(!temp.get("HPHM").equals(plate.get("HPHM").toString())&&lp.size()>0){
				result.setCode(ConstantRender.render_error_code);
				result.setMsg("号牌号码已存在！");
			}else{
				plate.update();
				
				Owner  owner = getModel(Owner.class,"owner");
				owner.update();
				
				vehicleparemeters.set(VehicleParemeters.column_HPHM, plate.get("HPHM"));
				vehicleparemeters.update();
				
//			Vehicle vehicle = getModel(Vehicle.class,"vehicle");
				vehicle.set(Vehicle.column_HPHM, plate.get("HPHM"));
				vehicle.set(Vehicle.column_OWNERNAME, owner.get("OWNERNAME"));
				vehicle.set(vehicle.column_OWNERTYPE, owner.getStr(Owner.column_OWNERTYPE));
				vehicle.update();
				
				Credentials credentials = getModel(Credentials.class,"credentials");
				credentials.set(Credentials.column_HPHM, plate.get("HPHM"));
				credentials.set(credentials.column_OWNERNAME, owner.get("OWNERNAME"));
//		credentials.set(Credentials.column_COMPANYNAME, owner.get("COMPANYNAME"));
				credentials.set(Credentials.column_ADDRESS, owner.get("ADDRESS"));
				credentials.set(Credentials.column_DETAILADDRESS, owner.get("DETAILADDRESS"));
				credentials.update();
				
				Business  business = getModel(Business.class,"business");
				business.set(Business.column_NEWOWNERNAME, owner.get("OWNERNAME"));
				business.set(business.column_NEWOWNERTYPE, owner.getStr(Owner.column_OWNERTYPE));
				business.set(business.column_NEWCARDNUMBER, owner.getStr(Owner.column_SFZHM));
				business.set(Business.column_NEWHPHM, plate.get("HPHM"));
				business.set(Business.column_YWXGDATE, sdf.format(new Date()));
				business.set(Business.column_YWXGJBRIDS, jbrids);
				business.set(Business.column_YWXGJBR, jbr);
				business.update();
				
				String picStr1 = getPara("picStr1");
				String picStr2 = getPara("picStr2");
				String picStr3 = getPara("picStr3");
				String picStr4 = getPara("picStr4");
				String BUSINESSIDS = business.get("IDS");
				String VEHICLEIDS = vehicle.get("IDS");
				if(picStr1.length()>0){
					pictureService.uploadPhoto(picStr1,BUSINESSIDS,VEHICLEIDS,"PICTURE1","PICTURE1");
				}
				if(picStr2.length()>0){
					pictureService.uploadPhoto(picStr2,BUSINESSIDS,VEHICLEIDS,"PICTURE2","PICTURE2");
				}
				if(picStr3.length()>0){
					pictureService.uploadPhoto(picStr3,BUSINESSIDS,VEHICLEIDS,"PICTURE3","PICTURE3");
				}
				if(picStr4.length()>0){
					pictureService.uploadPhoto(picStr4,BUSINESSIDS,VEHICLEIDS,"PICTURE4","PICTURE4");
				}
				result.setCode(ConstantRender.render_success_code);
				result.setMsg("保存成功!");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setCode(ConstantRender.render_error_code);
			result.setMsg("保存失败");
		}
		renderJson(result);
	}
}
